package com.yunfeisoft.business.service.impl;

import com.alibaba.fastjson.JSON;
import com.applet.base.BaseServiceImpl;
import com.yunfeisoft.business.dao.inter.SmsCouponDao;
import com.yunfeisoft.business.dao.inter.SmsCouponProductCategoryRelationDao;
import com.yunfeisoft.business.dao.inter.SmsCouponProductRelationDao;
import com.yunfeisoft.business.model.SmsCoupon;
import com.yunfeisoft.business.model.SmsCouponProductCategoryRelation;
import com.yunfeisoft.business.model.SmsCouponProductRelation;
import com.yunfeisoft.business.service.inter.SmsCouponProductCategoryRelationService;
import com.yunfeisoft.business.service.inter.SmsCouponProductRelationService;
import com.yunfeisoft.business.service.inter.SmsCouponService;
import com.applet.sql.separation.DataSourceChange;
import com.applet.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * ClassName: SmsCouponServiceImpl
 * Description: 优惠卷表service实现
 * Author: Jackie liu
 * Date: 2020-06-02
 */
@Service("smsCouponService")
public class SmsCouponServiceImpl extends BaseServiceImpl<SmsCoupon, String, SmsCouponDao> implements SmsCouponService {

    @Autowired
    private SmsCouponProductRelationService productRelationService;

    @Autowired
    private SmsCouponProductCategoryRelationService categoryRelationService;

    @Override
    @DataSourceChange(slave = true)
    public Page<SmsCoupon> queryPage(Map<String, Object> params) {
        return getDao().queryPage(params);
    }

    /**
     * 新增优惠券
     * @param couponStr
     */
    @Transactional
    @Override
    public void saveCoupon(String couponStr) {
        SmsCoupon coupon = JSON.parseObject(couponStr, SmsCoupon.class);
        // 设置默认值
        coupon.setCode(UUID.randomUUID().toString());
        coupon.setMemberLevel(0);
        coupon.setUseCount(0);
        coupon.setPublishCount(coupon.getCount());
        coupon.setReceiveCount(0);
        // 新增优惠券
        save(coupon);
        // 获取使用分类
        Integer useType = coupon.getUseType();
        if (1 == useType.intValue()) {
            // 指定类型
            List<SmsCouponProductCategoryRelation> categoryRelationList = coupon.getCategoryRelationList();
            categoryRelationList = categoryRelationList.stream().map(categoryRelation -> {
                categoryRelation.setCouponId(coupon.getId());
                return categoryRelation;
            }).collect(Collectors.toList());
            // 批量添加
            categoryRelationService.batchSave(categoryRelationList);
        } else if (2 == useType.intValue()) {
            // 指定商品
            List<SmsCouponProductRelation> productRelationList = coupon.getProductRelationList();
            productRelationList = productRelationList.stream().map(productRelation -> {
                productRelation.setCouponId(coupon.getId());
                return productRelation;
            }).collect(Collectors.toList());
            // 批量添加
            productRelationService.batchSave(productRelationList);
        }
    }

    /**
     * 修改优惠券信息
     * @param couponStr
     */
    @Override
    public Map<String, Object> modifyCoupon(String couponStr) {
        Map<String, Object> result = new HashMap<>();
        SmsCoupon coupon = JSON.parseObject(couponStr, SmsCoupon.class);
        if (StringUtils.isEmpty(coupon.getId())) {
            result.put("flag", false);
            result.put("message", "参数错误");
            return result;
        }
        // 修改优惠券信息
        modify(coupon);
        // 获取使用类型
        Integer useType = coupon.getUseType();
        if (1 == useType.intValue()) {
            // 先删除之前的分类优惠券关联信息
            categoryRelationService.deleteByCouponId(coupon.getId());
            // 指定分类
            List<SmsCouponProductCategoryRelation> categoryRelationList = coupon.getCategoryRelationList();
            categoryRelationList = categoryRelationList.stream().map(categoryRelation -> {
                categoryRelation.setCouponId(coupon.getId());
                categoryRelation.setId(null);
                return categoryRelation;
            }).collect(Collectors.toList());
            // 批量添加
            categoryRelationService.batchSave(categoryRelationList);
        } else if (2 == useType.intValue()) {
            // 先删除之前的商品优惠券关联信息
            productRelationService.deleteByCouponId(coupon.getId());
            // 指定商品
            List<SmsCouponProductRelation> productRelationList = coupon.getProductRelationList();
            productRelationList = productRelationList.stream().map(productRelation -> {
                productRelation.setCouponId(coupon.getId());
                productRelation.setId(null);
                return productRelation;
            }).collect(Collectors.toList());
            // 批量添加
            productRelationService.batchSave(productRelationList);
        }
        result.put("flag", true);
        result.put("message", "修改成功");
        return result;
    }

    /**
     * 查询商品优惠券列表
     * @param productId
     * @return
     */
    @Override
    public List<SmsCoupon> queryListByProductId(String productId) {
        // 查询指定商品的优惠券
        List<SmsCoupon> productCouponLList = getDao().queryListByProductId(productId);
        // 查询指定分类的优惠券
        List<SmsCoupon> categoryCouponLList = getDao().queryListByCategory(productId);
        // 查询所有通用的优惠券
        List<SmsCoupon> commonCouponList = getDao().queryListCommon();
        Set<SmsCoupon> set = new HashSet<>();
        set.addAll(productCouponLList);
        set.addAll(categoryCouponLList);
        set.addAll(commonCouponList);
        return new ArrayList<>(set);
    }
}